// pages/login/login.js
const app = getApp();
const md5 = require('../../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {},
  forget_pas() {
    wx.navigateTo({
      url: 'findPass/findPass'
    })
  },
  getVer: function() {
    var that = this;
    //判断是否为已注册手机号
    console.log('tel+' + this.data.telephone + ';ver+' + this.data.verify_code)
    wx.request({
      method: 'POST',
      url: app.globalData.url + 'api/sign/telephone/count',
      data: {
        telephone: this.data.telephone
      },
      success: function(e) {
        // console.log(e)
        //手机号格式正确
        if (e.data.code == 0) {
          //手机号已经注册
          if (e.data.data.count == 1) {
            //定义定时器开始时间
            that.setData({
              countdown: 60
            })
            //定时开始
            var timer = setInterval(function() {
              that.setData({
                countdown: that.data.countdown - 1
              })
              // console.log(that.data.countdown)
              if (that.data.countdown <= 0) {
                clearInterval(timer)
              }
            }, 1000)
            //开始请求
            wx.request({
              method: 'POST',
              url: app.globalData.url + 'api/sign/get/verify',
              data: {
                telephone: that.data.telephone,
                type: "forget"
              },
              success: function(e) {
                console.log(e)
              }
            })
          } else {
            //手机号未注册
            wx.showToast({
              title: '手机号未注册',
              icon: 'none'
            })
          }
        } else {
          //手机号格式错误
          wx.showToast({
            title: e.data.message,
            icon: 'none'
          })
        }

      }
    })
  },

  //用户账号
  user_input: function(e) {
    let telephone = e.detail.value;
    this.setData({
      telephone: telephone,
    })
    if (telephone && this.data.verify_code){
      this.setData({
        flag_user:true,
      })
    }else{
      this.setData({
        flag_user: false,
      })
    }
  },
  //验证码
  ver_input: function(e) {
    let verify_code = e.detail.value;
    this.setData({
      verify_code: verify_code,
    })
    if (verify_code && this.data.telephone) {
      this.setData({
        flag_user: true,
      })
    } else {
      this.setData({
        flag_user: false,
      })
    }
  },
  //忘记密码
  forget() {
    wx.request({
      url: app.globalData.url + 'api/sign/forget',
      data: {
        telephone: this.data.telephone,
        verify_code: this.data.verify_code,
        password: this.data.password,
        repet_password: this.data.repet_password,
      },
      method: 'POST',
      success: (e) => {
        console.log(e);
        let code = e.data.code,
          message = e.data.message;
        if (code == -1) {
          wx.showToast({
            title: message,
            icon: 'none'
          })
        } else {
          wx.showModal({
            content: '修改成功,请登入',
            showCancel: false,
            confirmText: '确定',
            confirmColor: '#d0a76c',
            success: (e) => {
              wx.redirectTo({
                url: '../../login/login'
              })
            }
          })
        }
      }
    })
  },
  //验证验证码
  check_reg() {
    wx.request({
      url: app.globalData.url + 'api/sign/verify/code',
      data: {
        telephone: this.data.telephone,
        verify_code: this.data.verify_code,
        type: 'forget',
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let code = e.data.code;
        if (code == 0) {
          this.setData({
            flag_next: true,
          })
        } else {
          wx.showToast({
            title: e.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  //下一步
  next_click: function() {
    let telephone = this.data.telephone,
      verify_code = this.data.verify_code;
    if (this.data.flag_user){
      if (telephone && verify_code) {
        this.check_reg();
      } else {
        wx.showToast({
          title: '必填项不能为空',
          icon: 'none'
        })
      }
    }else{
      return false;
    }
    
  },
  //确认
  confirm_click() {
    let inputOne = this.data.inputOne,
      inputTwo = this.data.inputTwo;
      if(this.data.flag_pass){
        if ((inputOne.length >= 6) && (inputTwo.length >= 6)) {
          this.forget();
        } else {
          wx.showToast({
            title: '密码长度须大于6位',
            icon: 'none'
          })
        }
      }else{
        return false;
      }
    

  },
  reg_inputOne: function(e) {
    // console.log(e)
    var password = md5.hex_md5(e.detail.value);
    var inputOne = e.detail.value;
    // console.log('One+' + password)
    this.setData({
      password: password,
      inputOne: e.detail.value,
    })
    if (inputOne && this.data.inputTwo){
      this.setData({
        flag_pass: true,
      })
    }else{
      this.setData({
        flag_pass: false,
      })
    }
  },
  reg_inputTwo: function(e) {
    var repet_password = md5.hex_md5(e.detail.value);
    var inputTwo = e.detail.value;
    // console.log('Two+' + password)
    this.setData({
      repet_password: repet_password,
      inputTwo: e.detail.value,
    })
    if (inputTwo && this.data.inputOne) {
      this.setData({
        flag_pass: true,
      })
    } else {
      this.setData({
        flag_pass: false,
      })
    }
  },
})